/*=========================================================================
Elite College as Engines of Upward Mobility: Evidence from Colombia's Ser Pilo Paga
Authors: Juliana Londoño-Vélez, Catherine Rodriguez, Fabio Sánchez
and Luis Esteban Álvarez-Arango

Creation date: June 6th, 2025
--------------------------------------------------------------------------
Tables A6 and A7
=========================================================================*/

use "${data}/data_RD", clear
keep if icfes_per==20142
glo cond_saber11	"eligible_sisben==1"
glo cond_sisben		"eligible_saber11==1"

********************************************************************************
*** ESTIMATION
********************************************************************************

foreach x in sector_9{
	glevelsof `x', local(sectores)
	foreach s of local sectores{
		gen `x'_`s'=(`x'=="`s'")
		replace `x'_`s'=. if `x'==""
	}
}

forvalues i=9/9{
	gen firm_micro_`i'=(firm_size_`i'==1)
	gen firm_peque_`i'=(firm_size_`i'==2)
	gen firm_mediana_`i'=(firm_size_`i'==3)
	gen firm_grande_`i'=(firm_size_`i'==4)
	foreach x in firm_micro_`i' firm_peque_`i' firm_mediana_`i' firm_grande_`i'{
		replace `x'=. if firm_size_`i'==.
	}
}

global outcomes dias0_9 dias_9 major_city_9 firm_micro_9 firm_peque_9 firm_mediana_9 firm_grande_9 numero_empleados_9 sector_9_A sector_9_B sector_9_C sector_9_DE sector_9_F sector_9_GHI sector_9_J sector_9_K sector_9_L sector_9_MN sector_9_OPQ sector_9_RST sector_9_U independiente_9 empleado_9

local NO: word count of $outcomes
local NO=`NO'-1
matrix r2014=J(`NO',34,.)

local i=1
foreach o of global outcomes{
	foreach r in saber11 sisben{
	if "`r'"=="saber11" local j=0
	if "`r'"=="sisben" 	local j=17
		
	cap rdrobust `o' running_`r' if ${cond_`r'}, masspoints(off) 
	if _rc==0{
	local bw=e(h_l)
	cap drop kw
	gen kw = ( (( (`bw') - abs(running_`r') ) /(`bw')) * (abs(running_`r') < (`bw')) )
	matrix r2014[`i',`=`j'+1']=e(tau_cl)
	matrix r2014[`i',`=`j'+2']=e(tau_bc)
	matrix r2014[`i',`=`j'+3']=e(se_tau_cl)
	matrix r2014[`i',`=`j'+4']=e(se_tau_rb)
	matrix r2014[`i',`=`j'+5']=e(ci_r_rb)
	matrix r2014[`i',`=`j'+6']=e(ci_l_rb)
	matrix r2014[`i',`=`j'+7']=e(tau_cl_l)
	matrix r2014[`i',`=`j'+8']=e(tau_bc_l)
	matrix r2014[`i',`=`j'+9']=e(pv_cl)
	matrix r2014[`i',`=`j'+10']=e(pv_rb)
	matrix r2014[`i',`=`j'+11']=e(h_l)
	matrix r2014[`i',`=`j'+12']=e(b_l)
	matrix r2014[`i',`=`j'+13']=e(N_h_l)
	matrix r2014[`i',`=`j'+14']=e(N_h_r)
	matrix r2014[`i',`=`j'+15']=e(N_b_l)
	matrix r2014[`i',`=`j'+16']=e(N_b_r)
	matrix r2014[`i',`=`j'+17']=e(N)
				
	}
	else {
		dis as error "`o' `r' could not be run"
	}
	}
	local i=1+`i'
}


***********************************
** Construcción Tablas de resultados
***********************************	

clear
svmat r2014
gen nombre=""
local NO: word count of $outcomes
local NO=`NO'-1
forvalues i=1/`NO'{
	local nn: word `i' of ${outcomes}
	replace nombre="`nn'" in `i'
}
order nombre
local nombres cc cr sec ser cs ci cc0 cr0 pc pr bwc bwr nec0 nec ner0 ner n
local nn: word count of `nombres'
local nn=`nn'-1
forvalues j=1/`nn'{
	local rn: word `j' of `nombres'
	rename r2014`j' `rn'_11
	rename r2014`=`j'+`nn'' `rn'_si
}

local runn 11 si
foreach x of local runn{
	label var cc_`x' "Coeficiente convencional"
	label var cr_`x' "Coeficiente robusto"
	label var sec_`x' "Error estándar convencional"
	label var ser_`x' "Error estándar robusto"
	label var cs_`x' "Intervalo superior robusto"
	label var ci_`x' "Intervalo inferior robusto"
	label var cc0_`x' "Media control convencional"
	label var cr0_`x' "Media control robusto"
	label var pc_`x' "Valor P convencional"
	label var pr_`x' "Valor P robusto"
	label var bwc_`x' "BW convencional"
	label var bwr_`x' "BW robusto"
	label var nec0_`x' "Observaciones efectivas izquierda convencional"
	label var nec_`x' "Observaciones efectivas derecha convencional"
	label var ner0_`x' "Observaciones efectivas izquierda robustas"
	label var ner_`x' "Observaciones efectivas derecha robustas"
	label var n_`x' "Observaciones"
}

save "${Intermediate}/Tab_A6_A7", replace
